<?php
namespace Home\Model;
use Think\Model;
class HomeUserModel extends Model{
    protected $_validate=array(
        array('user_name','require','用户名不能为空'),
        array('user_name','3,16','用户名必须为3到16位',2,'length'),
        array('pwd','require','密码不能为空'),
        array('pwd','3,16','密码必须为4到16位',2,'length'),
        array('repwd','pwd','确认密码与密码不匹配',0,'confirm'),
        array('email','email','邮箱格式不正确')

    );
//    protected $_auto=array(
//        array('salt','createSalt',3,'callback')
//    );
//    public function createSalt(){
//        return substr(str_shuffle('aljuudJOMG9875nkbtwslncwp'),0,8);
//        return md5(I('post.pwd').$salt);
//    }
    /**
     * @param $name
     * @param $pwd
     * @return bool
     */
    public function checkNamePwd($name,$pwd){
        if (empty($name) || empty($pwd)){
            return false;
        }
        $info=$this->where("user_name=".$name)->find();
        if ($info){
            $md5=$pwd.$info['salt'];
            $md=md5($md5);
            if (($info['pwd'] !== $md)){
                return false;
            }
            return true;
        }
    }
    
    /**
     * test code
     * @param $code
     * @return bool
     */
    public function check_verify($code){
        $verify = new \Think\Verify();
        return $verify->check($code);
    }

    /**
     * test user exists
     * @return bool
     */
    public function checkReg(){
        $info=$this->where('user_name='.I('post.user_name'))->select();
        if ($info){
            return false;
        }
        return true;
    }

    /**
     * test user login or none
     * @return bool
     */
    public function acc(){
        if (!isset($_SESSION['user_name']) || !isset($_SESSION['ccode'])) {
            return false;
        }
        $salt=C('SALT');
        return $_SESSION['ccode'] === md5($salt.'|||**'.$_SESSION['user_name']);
    }
}